//
//  Dock.m
//  宜居HD
//
//  Created by 张金国 on 16/4/26.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "Dock.h"
#import "Macro.h"
#import "Masonry.h"


@interface Dock ()

@property (nonatomic ,assign)int count;

@property (nonatomic ,strong)NSMutableArray *buttonImages;

@property (nonatomic ,strong)NSMutableArray *buttoncancleImages;

@property (nonatomic ,strong)NSMutableArray *buttons;
//临时记录选中的button
@property (strong,nonatomic)UIButton        *tmpBtn;

@end

@implementation Dock

#pragma mark - init
- (instancetype)initWithimages:(NSMutableArray *)images cancelImages:(NSMutableArray *)cancelImages andClickBlock:(DockButonClickBlock)dockButonClickBlock {
    if (self = [super init]) {
        self.backgroundColor = kDockBackgroundColor;
        self.count = (int)images.count;
        self.buttonImages = images;
        self.buttoncancleImages = cancelImages;
        self.dockButonClickBlock = dockButonClickBlock;
        [self setupConstraintsWithcount:self.count];
        self.tmpBtn = [self viewWithTag:UserMangermentButton];
        self.tmpBtn.selected = YES;
           }
    return self;
}

#pragma mark - Event Response
- (void)buttonAction:(UIButton *)button {
    DockButonType butonType = self.tmpBtn.tag;
    self.tmpBtn.selected = NO;
    if (self.tmpBtn != button) {
        self.tmpBtn = button;
        self.tmpBtn.selected = YES;
        self.tmpBtn.tag = button.tag;
        butonType = button.tag;
        DDLogInfo(@"self.tmpBtn.tag-------------%ld",self.tmpBtn.tag);
        self.dockButonClickBlock(butonType);
    }else {
        self.tmpBtn.selected = YES;
    }
   
}

#pragma mark - Private
- (void)setupConstraintsWithcount:(int) count {
    UIButton *tempbutton = nil;
    
    for (int i = 0; i < count; i ++ ) {
        UIButton  *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:self.buttoncancleImages[i] forState:UIControlStateNormal];
        [button setImage:self.buttonImages[i] forState:UIControlStateSelected];
        
        [self addSubview:button];
        if (tempbutton == nil) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.equalTo(self);
                make.height.equalTo(self.mas_height).multipliedBy(1/10.0f);
            }];
        }else {
            if (i == count-1) {
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.left.right.equalTo(self);
                    make.height.equalTo(self.mas_height).multipliedBy(1/10.0f);
                }];
            } else {
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(tempbutton.mas_bottom);
                    make.left.right.equalTo(tempbutton);
                    make.height.equalTo(self.mas_height).multipliedBy(1/10.0f);
                }];
            }
        }
        tempbutton = button;
        [self.buttons addObject:button];
    }
}

#pragma mark - getter
- (NSMutableArray *)buttonImages {
    if (!_buttonImages) {
        _buttonImages = [NSMutableArray array];
    }
    return _buttonImages;
}

- (NSMutableArray *)buttoncancleImages {
    if (!_buttoncancleImages) {
        _buttoncancleImages = [NSMutableArray array];
    }
    return _buttoncancleImages;
}

- (NSMutableArray *)buttons {
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

@end
